﻿using ConfExpert.Datasets;
using ConfExpert.Forms.Base;
using ConfExpert.Utility;

using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraReports.UI;
using DevTeam;

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo._7C
{
    public partial class CikisPartileri : XForm
    {
        int CKBatchId = -1;
        int BrandId;
        public CikisPartileri(int ckBatchId)
        {
            InitializeComponent();

            this.CKBatchId = ckBatchId;
            this.BrandId = AppSession.Lookups.CKBatch.FindByCKBatchId(CKBatchId).BrandId;
            this.Text += "- " + AppSession.Lookups.CKBatch.FindByCKBatchId(CKBatchId).Notes;
            //TAM.CikisPartisiTableAdapter = new Datasets.MyDataTableAdapters.CikisPartisiTableAdapter();
            TAM.CikisPartisiLinesTableAdapter = new Datasets.MyDataTableAdapters.CikisPartisiLinesTableAdapter();
        }

        BindingSource bs, bs2, bsx;
        private void CikisPartileri_Load(object sender, EventArgs e)
        {

            ds.CikisPartisiLines.Columns.Add("FARK", typeof(int));
            ds.CikisPartisi.Columns.Add("CikanAdet", typeof(int), "SUM(Child(FK_CikisPartisiLines_CikisPartisi_CikisPartisiId).CikanAdet)");
            ds.CikisPartisi.Columns.Add("KesilenAdet", typeof(int), "SUM(Child(FK_CikisPartisiLines_CikisPartisi_CikisPartisiId).KesilenAdet)");
            ds.CikisPartisi.Columns.Add("FARK", typeof(int), "SUM(Child(FK_CikisPartisiLines_CikisPartisi_CikisPartisiId).FARK)");

            bs = new BindingSource(ds, "CikisPartisi");
            bs2 = new BindingSource(bs, "FK_CikisPartisiLines_CikisPartisi_CikisPartisiId");
            bsx = new BindingSource(ds, "xCikisPartisiLines");

            InitGridView(gvCP, bs);
            InitGridView(gvCPL, bs2);
            InitGridView(gvEkle, bsx);

            gvCP.MakeRowSelectable();
            //gvCP.SutunEkle("ToplamAdet", typeof(int), "SUM(Child({FK}).CikanAdet)");
            AddBsToList(bs2);

            gvCPL.YanYanaGetirSagina("Modello", "Scan", "ProductId");

            gvEkle.MakeColumnInvisible("st", "AtelyeId");
            gleAtelye.Properties.Assign(RI(Rp.Atelye));
            gleAtelye.EditValueChanged += gleAtelye_EditValueChanged;

            gvCP.MakeReadOnly();
            gvCP.MakeColumnInvisible("CKBatchId");
            gvCP.FocusedRowChanged += gvCP_FocusedRowChanged;

            gvCPL.MakeReadOnly("Notlar");
            gvCPL.MakeColumnInvisible("CKBatchId", "CikisPartisiId", "CikisPartisiLinesId", "Modello_1", "Corpo");

            gvEkle.OptionsView.EnableAppearanceEvenRow = gvEkle.OptionsView.EnableAppearanceOddRow = false;
            gvEkle.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(gvEkle_RowStyle);
            gvEkle.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;

            GetDetails("CikisPartisi;CikisPartisiLines", "GetCikisPartileri", CKBatchId);

            gvEkle.AddSummary();
            gvCP.FormatAsNumber(0, "CikanAdet", "KesilenAdet", "FARK");
            gvCP.AddSummary("CikanAdet", "KesilenAdet", "FARK");
            gvCP.YanYanaGetirSoluna("UserId", "CikanAdet", "KesilenAdet", "FARK");
            gvCP.BestFitColumns();

            gvCPL.FormatAsNumber(0, "CikanAdet", "KesilenAdet", "FARK");
            gvCPL.AddSummary("CikanAdet", "KesilenAdet", "FARK");
            gvCPL.YanYanaGetir("FARK:YuklenenAdet");
            gvCPL.BestFitColumns();

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            btnEkle.Click += new EventHandler(btnEkle_Click);
            btnEkle.Enabled = false;

            GridManager gm3 = GridManager.GetManager(gvCP);
            gm3.GridMenu.AddMenu("Kesim Föyü- TR", btnKF_Click);
            gm3.GridMenu.AddMenu("Kesim Föyü- IT", btnKFIt_Click);
            gm3.GridMenu.AddMenu("Çıkış için Top Seç", TopCikisi_Click);
            gm3.GridMenu.AddMenu("İhtiyaç + Çıkılan", btnIhtiyac_Click);
            gm3.GridMenu.AddMenu("Kumaş Enleri Raporu", btnEnRapor_Click);
            gm3.GridMenu.AddMenu("Kumaşların Geçtiği Modeller", KullanimYeri_Click);
            gm3.GridMenu.AddMenu("Alternatif Artikel Kullanımı", Alternatifler_Click);

            if (ytk(79))    //Orhan+Deniz
            {
                gm3.GridMenu.AddMenu("Execution EKLE", AddExec_Click);
                gm3.GridMenu.AddMenu("Execution ÇIKAR", RemoveExec_Click);
                gm3.GridMenu.AddMenu("Başka atölyeye KAYDIR", Kaydir_Click);

                //if (this.Text.Contains("SAMPLE. "))
                //    gm3.GridMenu.AddMenu("Parça çıkış emri oluştur", PCE_Click);
            }
            else
            {
                splitContainerControl1.Panel1.Visible = false;
                ExpandCollapse(splitContainerControl1, AcKapa.Collapse);
                btnEkle.Visible = false;
            }

            splitContainerControl2.AutoSize();
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Save();
        }


        void gvCP_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gvCPL.BestFitColumns();
        }

        void gleAtelye_EditValueChanged(object sender, EventArgs e)
        {
            btnEkle.Enabled = intParse(gleAtelye.EditValue) > 0;
        }

        void TopCikisi_Click(object sender, EventArgs e)
        {
            ExecuteNonQuery("SepetiBosalt", WhoAmI);
            AppSession.MainForm.ShowMdiChildForm(typeof(DepoStok), intParse(gvCP.GetFocusedRowCellValue("CikisPartisiId")));
        }

        void btnIhtiyac_Click(object sender, EventArgs e)
        {
            using (Executions exe = new Executions(CKBatchId, intParse(gvCP.GetFocusedRowCellValue("CikisPartisiId"))))
            {
                exe.ShowDialog();
            }
        }


        void KullanimYeri_Click(object sender, EventArgs e)
        {
            AppSession.MainForm.ShowMdiChildForm(typeof(KullanimYeri), intParse(gvCP.GetFocusedRowCellValue("CikisPartisiId")));
        }

        void AddExec_Click(object sender, EventArgs e)
        {
            EkleCikar(1);
        }

        void RemoveExec_Click(object sender, EventArgs e)
        {
            EkleCikar(-1);
        }

        private void EkleCikar(int yon)
        {
            int CikisPartisiId = intParse(gvCP.GetFocusedRowCellValue("CikisPartisiId"));
            using (EditCP ecp = new EditCP(CikisPartisiId, yon))
                if (ecp.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ds.CikisPartisi.Clear();
                    ds.CikisPartisiLines.Clear();
                    GetDetails("CikisPartisi;CikisPartisiLines", "GetCikisPartileri", CKBatchId);

                    gvCP.FocusedRowHandle = gvCP.LocateByValue(0, gvCP.Columns["CikisPartisiId"], CikisPartisiId);
                    gvCP.MakeRowVisible(gvCP.FocusedRowHandle);
                }
        }


        void Kaydir_Click(object sender, EventArgs e)
        {
            int CikisPartisiId = intParse(gvCP.GetFocusedRowCellValue("CikisPartisiId"));
            using (DosyaKaydir dk = new DosyaKaydir(CikisPartisiId))
                if (dk.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ds.CikisPartisi.Clear();
                    ds.CikisPartisiLines.Clear();
                    GetDetails("CikisPartisi;CikisPartisiLines", "GetCikisPartileri", CKBatchId);

                    gvCP.FocusedRowHandle = gvCP.LocateByValue(0, gvCP.Columns["CikisPartisiId"], CikisPartisiId);
                    gvCP.MakeRowVisible(gvCP.FocusedRowHandle);
                }
        }

        void Alternatifler_Click(object sender, EventArgs e)
        {
            AppSession.MainForm.ShowMdiChildForm(typeof(AlternatifKullanimi), intParse(gvCP.GetFocusedRowCellValue("CikisPartisiId")));
        }

        void btnEnRapor_Click(object sender, EventArgs e)
        {
            xrCikilanEnler report = new xrCikilanEnler(intParse(gvCP.GetFocusedRowCellValue("CikisPartisiId")));
            report.ShowPreview();
        }

        void btnEn_Click(object sender, EventArgs e)
        {
            using (CikilanEnler exe = new CikilanEnler(intParse(gvCP.GetFocusedRowCellValue("CikisPartisiId"))))
            {
                exe.ShowDialog();
            }
        }

        void gvEkle_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle > -1)
                if ((sender as GridView).GetRowCellValue(e.RowHandle, "st") != null)
                    if ((sender as GridView).GetRowCellValue(e.RowHandle, "st") != DBNull.Value)
                        switch (intParse((sender as GridView).GetRowCellValue(e.RowHandle, "st")))
                        {
                            case 1:
                                e.Appearance.BackColor = Color.DeepSkyBlue;
                                break;
                            case 2:
                                e.Appearance.BackColor = Color.YellowGreen;
                                break;
                        }
        }

        void btnEkle_Click(object sender, EventArgs e)
        {
            string str = string.Empty;
            List<string> lst = new List<string>();
            foreach (MyData.xCikisPartisiLinesRow r in ds.xCikisPartisiLines)
                lst.Add(r.Modello);
            str = string.Join(",", lst.ToArray());

            GetDetails("xCikisPartisiLines;CikisPartisi;CikisPartisiLines", "InsCikisPartisiYeni3", CKBatchId, WhoAmI, str, intParse(gleAtelye.EditValue), 0);
            gvEkle.BestFitColumns();
            if (ds.xCikisPartisiLines.Select("st is not null").Length == 0)
            {
                ds.xCikisPartisiLines.Clear();

                gvCP.MoveLast();
                gvCP.MakeRowVisible(gvCP.FocusedRowHandle);
                gvCPL.BestFitColumns();
            }
            else
                XtraMessageBox.Show("Girilenler içinde, boş bırakılmış veya daha önce üretilmiş execution satır(lar)ı olamaz.", "Hata");
        }

        void CikisPartisi_TableNewRow(object sender, System.Data.DataTableNewRowEventArgs e)
        {
            e.Row["CKBatchId"] = CKBatchId;
            e.Row["UserId"] = WhoAmI;
        }

        void btnKF_Click(object sender, EventArgs e)
        {
            int CikisPartisiId = intParse(bs, "CikisPartisiId");
            if (CikisPartisiId > 0)
            {
                PrintKesimBilgiFormu(BrandId, CikisPartisiId, "TR");

                xrKesim barcode = new xrKesim(BrandId, CikisPartisiId, "TR");
                barcode.ShowPreview();
            }
        }

        void btnKFIt_Click(object sender, EventArgs e)
        {
            int CikisPartisiId = intParse(bs, "CikisPartisiId");
            if (CikisPartisiId > 0)
            {
                PrintKesimBilgiFormu(BrandId, CikisPartisiId, "IT");

                xrKesim barcode = new xrKesim(BrandId, CikisPartisiId, "IT");
                barcode.ShowPreview();
            }
        }

        void PrintKesimBilgiFormu(int BrandId, int CikisPartisiId, string Lang)
        {
            xrTopluBedenDagilimi xr = new xrTopluBedenDagilimi(BrandId, CikisPartisiId, Lang);
            xr.CreateDocument();
            xr.PrintingSystem.ContinuousPageNumbering = true;
            ReportPrintTool printTool2 = new ReportPrintTool(xr);
            //xr.PrintingSystem.StartPrint += new DevExpress.XtraPrinting.PrintDocumentEventHandler(PrintingSystem_StartPrint);
            //xr.Landscape = false;
            printTool2.ShowPreview();
        }
    }
}